package com.wbl.train.common.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * Author: 王博纶
 * Date: 2024/1/31 11:06
 * FileName: JacksonConfig
 * Description: json格式化转换 --- 全局配置 --- 微服务以bean对象的方式注册
 * 需要全局使用的时候，将@Bean的注释取消即可
 */
@Configuration
public class JacksonConfig {

//    @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        //创建一个映射器
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        SimpleModule simpleModule = new SimpleModule();
        //序列化---将Long类型的数据转换成String类型的数据相应出去---保证响应给前端的时候精度不会丢失
        simpleModule.addSerializer(Long.class,ToStringSerializer.instance);
        // 设置响应数据为null时不序列化---NOT_NULL
        // JsonInclude.Include.NON_DEFAULT最强过滤，只要没有赋值就都过滤
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        //将创建修改的转换器注册进去
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}
